// 全局变量
let currentUserId = null;
let currentPage = 1;
const pageSize = 10;

// DOM元素
let loginModal, registerModal, publishQuestionModal, rechargeModal;

// 初始化函数
function init() {
    // 获取模态框元素
    loginModal = document.getElementById('loginModal');
    registerModal = document.getElementById('registerModal');
    publishQuestionModal = document.getElementById('publishQuestionModal');
    rechargeModal = document.getElementById('rechargeModal');
    
    // 添加点击模态框外部关闭的事件监听
    window.onclick = handleModalClick;
    
    // 更新导航状态
    updateNavigation();
    
    console.log('页面初始化完成');
}

// 页面导航函数
function showHome() {
    document.getElementById('homeContent').style.display = 'block';
    document.getElementById('questionList').style.display = 'none';
    document.getElementById('userInfoContent').style.display = 'none';
}

function showQuestionList() {
    document.getElementById('homeContent').style.display = 'none';
    document.getElementById('questionList').style.display = 'block';
    document.getElementById('userInfoContent').style.display = 'none';
    
    // 加载问题列表
    currentPage = 1;
    loadQuestions(currentPage);
}

function showUserInfo() {
    if (!currentUserId) {
        alert('请先登录');
        return;
    }
    
    document.getElementById('homeContent').style.display = 'none';
    document.getElementById('questionList').style.display = 'none';
    document.getElementById('userInfoContent').style.display = 'block';
    
    // 加载用户信息
    loadUserInfo();
    // 加载用户发布的问题
    loadUserQuestions();
}

// 更新导航状态
function updateNavigation() {
    const userInfoLink = document.getElementById('userInfoLink');
    const loginLink = document.getElementById('loginLink');
    const registerLink = document.getElementById('registerLink');
    const logoutLink = document.getElementById('logoutLink');
    
    // 检查是否有登录信息（实际项目中可能需要从localStorage或cookie中获取）
    const savedUserId = localStorage.getItem('userId');
    if (savedUserId) {
        currentUserId = savedUserId;
        userInfoLink.style.display = 'block';
        logoutLink.style.display = 'block';
        loginLink.style.display = 'none';
        registerLink.style.display = 'none';
        
        // 如果当前在个人中心页面，重新加载信息
        if (document.getElementById('userInfoContent').style.display === 'block') {
            loadUserInfo();
            loadUserQuestions();
        }
    } else {
        userInfoLink.style.display = 'none';
        logoutLink.style.display = 'none';
        loginLink.style.display = 'block';
        registerLink.style.display = 'block';
    }
}

// 处理模态框点击事件
function handleModalClick(event) {
    if (event.target === loginModal) {
        closeLoginModal();
    } else if (event.target === registerModal) {
        closeRegisterModal();
    } else if (event.target === publishQuestionModal) {
        closePublishQuestionModal();
    } else if (event.target === rechargeModal) {
        closeRechargeModal();
    }
}

// 辅助函数：显示消息
function showMessage(elementId, message, isSuccess) {
    const element = document.getElementById(elementId);
    if (element) {
        element.textContent = message;
        element.className = 'alert ' + (isSuccess ? 'alert-success' : 'alert-danger');
        element.style.display = 'block';
        
        // 3秒后自动隐藏
        setTimeout(() => {
            element.style.display = 'none';
        }, 3000);
    }
}

// 辅助函数：格式化日期
function formatDate(dateString) {
    if (!dateString) return '';
    const date = new Date(dateString);
    return date.toLocaleString('zh-CN');
}

// 辅助函数：获取状态文本
function getStatusText(status) {
    switch(status) {
        case 0: return '待解决';
        case 1: return '已解决';
        case 2: return '已关闭';
        default: return '未知';
    }
}

// 辅助函数：截断文本
function truncateText(text, maxLength) {
    if (!text || text.length <= maxLength) return text;
    return text.substring(0, maxLength) + '...';
}

// 页面加载完成后初始化
window.onload = init;